package com.sky.controller.admin;

import com.sky.dto.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@Api(tags = "订单管理")
@RequestMapping("/admin/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO pageQueryDTO) {
        log.info("订单搜索:{}", pageQueryDTO);
        PageResult pageResult = orderService.conditionSearch(pageQueryDTO);
        return Result.success(pageResult);

    }

    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> getById(@PathVariable Long id) {
        log.info("查询订单详情");
        OrderVO detail = orderService.getOrderAndDetailById(id);
        return Result.success(detail);

    }

    @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO dto) {
        log.info("接单:{}", dto);
        orderService.acceptOrders(dto);
        return Result.success();

    }

    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO dto) {
        log.info("接单:{}", dto);
        orderService.rejectionOrders(dto);
        return Result.success();

    }

    @ApiOperation("订单取消")
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO cancelDTO) {
        log.info("取消订单:{}", cancelDTO);
        orderService.adminCancelById(cancelDTO);
        return Result.success();
    }

    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable Long id) {
        log.info("派送订单：{}", id);
        orderService.delivery(id);
        return Result.success();

    }

    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id) {
        log.info("完成订单：{}", id);
        orderService.complete(id);
        return Result.success();
    }

    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        log.info("统计各个状态的订单数量");
        OrderStatisticsVO overViewVO = orderService.statistics();
        return Result.success(overViewVO);
    }
}
